<?php


namespace app\common\model;


use think\Model;
use think\facade\Db;
class Topic extends Model
{

    public function userInfo()
    {
        return $this->hasOne("User","uid","uid");
    }

    public static function getList($where = "", $order = "id desc", $paginate = 10)
    {
        $list = self::withJoin(["userInfo"])->where($where)->order($order)->paginate($paginate)->each(function ($item){
            $topicId = $item["id"];
            $topicPostCount = Db::name("post")->where("topic_id",$topicId)->count();
            $topicUserCount = Db::name("user_topic")->where("topic_id",$topicId)->count();

            $item['post_count'] = $topicPostCount;
            $item['user_count'] = $topicUserCount;
            return $item;
        });


        $list->hidden(['openid'])->toArray();
        return $list;
    }

    public static function getListAll($where = "", $order = "id desc")
    {
        $list = self::withJoin(["userInfo"])->where($where)->order($order)->select()->each(function ($item){
            $topicId = $item["id"];
            $topicPostCount = Db::name("post")->where("topic_id",$topicId)->count();
            $topicUserCount = Db::name("user_topic")->where("topic_id",$topicId)->count();

            $item['post_count'] = $topicPostCount;
            $item['user_count'] = $topicUserCount;
            return $item;
        });


        $list->hidden(['openid'])->toArray();
        return $list;
    }
}